package com.ansion.utils;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class ArrayUtil {


    public static <T extends Object> String combine(@NonNull List<T> list) {
        if (list.isEmpty())
            return "";
        StringBuilder sb = new StringBuilder();
        for (T t : list) {
            sb.append(t.toString()).append(",");
        }
        //删去最后一个字符
        sb.deleteCharAt(sb.toString().length() - 1);
        return sb.toString();
    }


    public static <T extends Object> List<List<T>> split(List<T> list, int count) {
        if (isEmpty(list))
            return Collections.emptyList();
        if (list.size() <= count)
            return Collections.singletonList(list);
        List<List<T>> splitList = new ArrayList<>();
        int size = list.size();
        int page = (size + count - 1) / count;

        for (int i = 0; i < page; i++) {
            List<T> dataList = null;
            if (i == page - 1) {//最后一页
                dataList = list.subList(i * count, size);
            } else {
                dataList = list.subList(i * count, (i + 1) * count);
            }
            splitList.add(dataList);
        }
        return splitList;
    }

    public static String combine(@NonNull Object... params) {
        if (params.length == 0)
            return "";
        StringBuilder sb = new StringBuilder();
        for (Object t : params) {
            sb.append(t.toString()).append(",");
        }
        //删去最后一个字符
        sb.deleteCharAt(sb.toString().length() - 1);
        return sb.toString();
    }

    public static String[] split(String str) {
        if (TextUtils.isEmpty(str))
            return new String[0];
        String[] arrays = str.split(",");
        return arrays;
    }

    public static <T extends Object> List<T> split(String str, @NonNull ValueTransfer<T> transfer) {
        if (TextUtils.isEmpty(str))
            return Collections.emptyList();
        String[] arrays = str.split(",");
        return ValueSafeTransfer.transformList(Arrays.asList(arrays), (position, t) -> transfer.transferValue(t));

    }

    public static boolean isEmpty(Collection c) {
        return c == null || c.isEmpty();
    }

    public static boolean isEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    public static boolean isEmpty(int[] arr) {
        if (arr != null && arr.length > 0)
            return false;
        return true;
    }

    public static boolean isEmpty(String[] arr) {
        if (arr != null && arr.length > 0)
            return false;
        return true;
    }

    public static boolean isEmpty(long[] arr) {
        if (arr != null && arr.length > 0)
            return false;
        return true;
    }

    public static boolean isEmpty(byte[] arr) {
        if (arr != null && arr.length > 0)
            return false;
        return true;
    }

    public static boolean isEmpty(char[] arr) {
        if (arr != null && arr.length > 0)
            return false;
        return true;
    }

    public static boolean isEmpty(Object[] arr) {
        if (arr != null && arr.length > 0)
            return false;
        return true;
    }

    public static <K, V> boolean mapIsSame(Map<K, V> map1, Map<K, V> map2) {
        if (map1.size() != map2.size()) {
            return false;
        }

        for (Map.Entry<K, V> entry : map1.entrySet()) {
            K key = entry.getKey();
            V value1 = entry.getValue();
            V value2 = map2.get(key);

            if (!Objects.equals(value1, value2)) {
                return false;
            }
        }

        return true;
    }

    public interface ValueTransfer<T> {
        @NonNull
        T transferValue(String value);
    }
}
